﻿//----------------------------------------------------------------------------------------------------------------------
// <copyright file="GrayscaleEffect.cs" company="Keith B. Rimington">
//
//   Copyright (c) 2011 Keith B. Rimington
//
//   Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
//     documentation files (the "Software"), to deal in the Software without restriction, including without limitation
//     the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and
//     to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
//   The above copyright notice and this permission notice shall be included in all copies or substantial portions of
//     the Software.
//
//   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
//     THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
//     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
//     CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
//     DEALINGS IN THE SOFTWARE.
//
// </copyright>
//----------------------------------------------------------------------------------------------------------------------

namespace Noc.Pomodoro.Shaders
{
    using System;
    using System.Windows;
    using System.Windows.Media;
    using System.Windows.Media.Effects;

    /// <summary>
    /// Simple grayscale pixel shader effect, courtesy of several online examples.
    /// </summary>
    public class GrayscaleEffect : ShaderEffect
    {
        /// <summary>
        /// Backing store for the Input property.
        /// </summary>
        public static readonly DependencyProperty InputProperty = ShaderEffect.RegisterPixelShaderSamplerProperty("Input", typeof(GrayscaleEffect), 0);

        /// <summary>
        /// Backing store for the SaturationFactor property.
        /// </summary>
        public static readonly DependencyProperty SaturationFactorProperty = DependencyProperty.Register("SaturationFactor", typeof(double), typeof(GrayscaleEffect), new UIPropertyMetadata(0.0, PixelShaderConstantCallback(0), CoerceSaturationFactor));

        /// <summary>
        /// Singleton shader instance.
        /// </summary>
        private static readonly PixelShader Shader = new PixelShader() { UriSource = new Uri(@"pack://application:,,,/Pomodoro.Shaders;component/GrayscaleEffect.ps") };

        /// <summary>
        /// Initializes a new instance of the <see cref="GrayscaleEffect"/> class.
        /// </summary>
        public GrayscaleEffect()
        {
            PixelShader = Shader;

            UpdateShaderValue(InputProperty);
            UpdateShaderValue(SaturationFactorProperty);
        }

        /// <summary>
        /// Gets or sets the input value for the shader effect.
        /// </summary>
        /// <value>
        /// The input value.
        /// </value>
        public Brush Input
        {
            get { return (Brush)GetValue(InputProperty); }
            set { SetValue(InputProperty, value); }
        }

        /// <summary>
        /// Gets or sets the saturation factor, between 0.0 and 1.0.
        /// Use 0.0 for pure grayscale, 1.0 for full color, and middle
        /// values for muted colors.
        /// </summary>
        /// <value>
        /// The saturation factor.
        /// </value>
        public double SaturationFactor
        {
            get { return (double)GetValue(SaturationFactorProperty); }
            set { SetValue(SaturationFactorProperty, value); }
        }

        /// <summary>
        /// I'm not really sure what this is for. It was part of the example, and I've joined the cargo cult.
        /// </summary>
        /// <param name="d">A dependency object.</param>
        /// <param name="value">The assigned value.</param>
        /// <returns>A new value.</returns>
        private static object CoerceSaturationFactor(DependencyObject d, object value)
        {
            GrayscaleEffect effect = (GrayscaleEffect)d;
            double newFactor = (double)value;

            if (newFactor < 0.0 || newFactor > 1.0)
            {
                return effect.SaturationFactor;
            }

            return newFactor;
        }
    }
}